package com.example.core.service;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.core.dao.SysLicenseParamDao;
import com.example.core.po.SysLicenseParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/****
 *
 * 定义保存许可申请信息
 */
@Service
public class SysLicenseParamServiceImpl extends ServiceImpl<SysLicenseParamDao, SysLicenseParam> implements SysLicenseParamService {

    @Value("${sitc.path}")
    private String dirPath;
    @Resource
    private SysKeyStoreService sysKeyStoreService;
    @Resource
    private SysLicenseManagerService sysLicenseManagerService;

    @Override
    public byte[] download(String id) throws Exception {
        SysLicenseParam sysLicenseParam = this.getById(id);
        if (sysLicenseParam == null){
            throw new Exception("id: " + id + "对应的许可申请不存在...");
        }
        this.sysKeyStoreService.create(sysLicenseParam);
        this.sysLicenseManagerService.createLicense(sysLicenseParam);
        File targetFile = new File(dirPath + sysLicenseParam.getLicPath());
        if (!targetFile.exists()){
            throw new Exception("不存在许可文件，请稍后再试...");
        }
        return FileUtil.readBytes(targetFile);
    }
}
